import util from "../../../utils/util";
const app = getApp();
var citycode = "sz";
let nowMonth = new Date().getMonth() + 1;
Page({

    data: {
        moonth: [
            []
        ],
        type: ["全部活动"],
        tabIndex: 0,
        moonthIndex: "0",
        lunbo: [],
        typeIndex: 0,
        screenHeight: "",
        showMask: false,
        input: "",
        free: 0,
        citycode: "sz",
        cityname: "深圳",
        showtk: false,
        area: [],
        areaIndex: 0,
        jingqu: [],
        jingquIndex: 0,
        textIndex: "",
        showList: true,
    },

    onLoad: function (options) {
        let systemInfo = ks.getSystemInfoSync();
        this.setData({
            screenHeight: systemInfo.safeArea.height - systemInfo.statusBarHeight,
            citycode: options.citycode ? options.citycode : ks.getStorageSync('citycode') ? ks.getStorageSync('citycode') : 'sz',
            cityname: options.cityname ? options.cityname : ks.getStorageSync('city') ? ks.getStorageSync('city') : '深圳',
            tabIndex: options.type ? options.type : 0,
            jqid: options.jingdian,
            areaid: options.areaid ? options.areaid : ''
        });
    },

    onShow() {
        this.setData({
            areaIndex: 0,
            jingquIndex: 0,
        });
        if (this.data.tabIndex == 0)
            this.getActiveInfo().then(res => {
                if (res.state == 1) {
                    if (res.data.active)
                        res.data.active.unshift("全部活动");
                    this.setData({
                        moonth: res.data.month,
                        type: res.data.active,
                    });
                    this.data.moonth[this.data.moonthIndex][1] == nowMonth ? this.setData({
                        showList: true
                    }) : this.setData({
                        moonthIndex: 0,
                        showList: false
                    });
                    this.getActiveInfo(this.data.moonth[this.data.moonthIndex][0]);
                }
            });
        else if (this.data.tabIndex == 1) {
            this.getInfo();
        }
    },

    // 获取活动信息
    getActiveInfo(mon = "") {
        ks.showLoading();
        return new Promise(resolve => {
            let data = {
                action: "huodong",
                citycode: this.data.citycode,
                chouse: 1,
                mon,
                huodong: this.data.input,
                active: this.data.typeIndex == 0 ? '' : this.data.type[this.data.typeIndex],
                free: this.data.free,
                type: "city",
            };

            util.getUrl(app.globalData.url).then(res => {
                util.request("huodong.php", data, "POST").then(res => {
                    if (res.data.state == 1) {
                        if (res.data.data.data) {
                            res.data.data.data.forEach(item => {
                                item.actdate = item.actdate.split(" ")[0];
                                item.expiredate = item.expiredate.split(" ")[0];
                                item.showDetail = false;
                                if (new Date(item.expiredate).getTime())
                                    item.com_yytime = `${item.actdate}-${item.expiredate}`
                                else
                                    item.com_yytime = `${item.actdate}起`
                            });
                        }
                        this.setData({
                            lunbo: res.data.data.lunbo,
                            active: res.data.data.data,
                        });
                        ks.hideLoading();
                        this.getnewlist();
                    }

                    return resolve(res.data);
                });
            });
        });
    },

    // 获取景点信息
    getInfo() {
        ks.showLoading();
        let area;
        if (this.data.areaid) {
            area = this.data.areaid;
        } else if (this.data.area[0]) {
            if (this.data.area[this.data.areaIndex].id) {
                area = this.data.area[this.data.areaIndex].id;
            } else {
                area = '';
            }
        } else {
            area = '';
        }
        let data = {
            action: "huodong",
            citycode: this.data.citycode,
            chouse: 2,
            huodong: this.data.input,
            area,
            jingdian: this.data.jingqu ? (this.data.jingqu[0] ? this.data.jingqu[this.data.jingquIndex].id : this.data.jqid) : "",
            type: "jingqu",
        };
       
        util.request("huodong.php", data, "POST").then(res => {
            if (res.data.state == 1) {
                ks.hideLoading();
                if (res.data.data.area)
                    res.data.data.area.unshift({
                        id: "",
                        cityname: "区域"
                    });
                if (res.data.data.class) {
                    res.data.data.class.unshift({
                        id: "",
                        classname: "全部"
                    });
                    res.data.data.class.forEach(item => {
                        if (item.id)
                            item.classname = item.classname.split("|")[1];
                    });
                }

                if (res.data.data.data) {
                    res.data.data.data.forEach(item => {
                        item.com_price = item.com_price ? item.com_price.replace(/\r\n/g, "") : "";
                        item.com_price = item.com_price ? item.com_price.replace(/\n/g, "") : "";
                    });
                }
                this.setData({
                    area: res.data.data.area
                })
                if (this.data.areaid)
                    if (this.data.area.some(item => item.id == this.data.areaid)) {
                        this.data.areaIndex = this.data.area.findIndex(item => item.id == this.data.areaid);
                        this.data.areaid = '';
                        this.setData({
                            areaIndex:this.data.areaIndex,
                            areaid:this.data.areaid
                        })
                    } else {
                        this.data.areaid = '';
                        this.setData({
                            areaid: this.data.areaid
                        })
                        ks.showToast({
                            title: '改区域暂无景点数据',
                            icon: 'none',
                            duration: 1000,
                            success: () => setTimeout(() => this.getInfo(), 1000)
                        });
                    }
                    this.setData({
                        jingqu:res.data.data.class,
                        active:res.data.data.data,
                        lunbo: res.data.data.lunbo
                    })
                if (this.data.jqid)
                    if (this.data.jingqu.some(item => item.id == this.data.jqid)) {
                        this.data.jingquIndex = this.data.jingqu.findIndex(item => item.id == this.data.jqid);
                        this.data.jqid = '';
                        this.setData({
                            jingquIndex:this.data.jingquIndex,
                            jqid:this.data.jqid
                        })
                    } else {
                        this.data.jqid = '';
                        this.setData({
                            jqid:this.data.jqid
                        })
                        ks.showToast({
                            title: '该城市暂无该景点类型',
                            icon: 'none',
                            duration: 1000,
                            success: () => setTimeout(() => this.getInfo(), 1000)
                        });
                    }
                    
                this.getnewlist();
            }
        }).catch(err => ks.hideLoading());
    },

    // 顶部导航栏切换
    barClick(e) {
        let index = util.getValue("index", e);
        this.setData({
            tabIndex: index,
        });
        if (index == 0)
            this.getActiveInfo(this.data.moonth[this.data.moonthIndex][0]).then(res => {
                this.setData({
                    moonth: res.data.month,
                })
            });
        else {
            this.getInfo();
            this.setData({
                showList: false,
            });
        }
    },

    // 月份切换
    moonth(e) {
        let index = util.getValue("index", e);
        if (this.data.moonth[index][1] == nowMonth)
            this.setData({
                showList: true
            })
        else
            this.setData({
                showList: false
            })
        this.setData({
            moonthIndex: index,
        });
        this.getActiveInfo(this.data.moonth[this.data.moonthIndex][0]);
    },

    // 活动类型切换
    typeChange(e) {
        let index = e.detail.value;
        this.setData({
            typeIndex: index,
        });
        this.getActiveInfo(this.data.moonth[this.data.moonthIndex][0]).then(res => {
            if (res.state == 1)
                this.setData({
                    lunbo: res.data.lunbo,
                    active: res.data.data,
                })
        });
    },

    // 展示浮层
    openMask(e) {
        let index = util.getValue("index", e);
        let data = this.data.active[index];
        this.detailIndex = index;
        this.setData({
            showMask: true,
            data,
        });
    },

    input(e) {
        let input = e.detail.value;
        this.setData({
            input
        });

        if (this.data.tabIndex == 0)
            this.getActiveInfo(this.data.moonth[this.data.moonthIndex][0]);
        else if (this.data.tabIndex == 1)
            this.getInfo();
    },

    // 只看免费
    free() {
        this.setData({
            free: this.data.free ^ 1,
        });
        this.getActiveInfo(this.data.moonth[this.data.moonthIndex][0]);
    },

    // 最新阅读的接口
    getnewlist(siteid) {
        let newData = {
            citycode: this.data.citycode,
            action: 'list',
            pagesize: 50,
            siteid: siteid,
            path: "article",
            city: this.data.cityname ? this.data.cityname : ks.getStorageSync("city"),
            keywords: this.data.typeIndex == 0 ? "活动" : this.data.type[this.data.typeIndex],
        };
        util.request('index.php', newData, 'GET').then(res => {
            this.setData({
                newlist: res.data,
            })
        })
    },

    seeDetail(e) {
        let data = this.data.active[this.detailIndex];
        let id = util.getValue("url", e);
        let url = util.getValue("url", e);
        let title;
        if (this.data.tabIndex == 0) {
            title = `${data.title}${data.type}入口`
        } else {
            title = `${data.com_name}预约入口`
        }

        this.setData({
            showtk: true
        })
        util.navContact(title, url)

    },

    // 进入客服会话
    contact() {
        this.setData({
            showtk: false,
        })
    },

    // 关闭
    close() {
        this.setData({
            showtk: false,
        });
    },

    closeMask() {
        this.setData({
            showMask: false,
        })
    },

    showText(e) {
        let index = util.getValue("index", e);
        this.data.active[index].showDetail = !this.data.active[index].showDetail;
        this.setData({
            active: this.data.active
        });
    },

    toMap(e) {
        let index = util.getValue("index", e);
        let text = util.getValue("text", e);
        let data = this.data.active[index];

        ks.setClipboardData({
            data: text,
            success: (result) => {
                setTimeout(() => {
                    if (this.data.tabIndex == 0) {
                        if (data.jd && data.wd)
                            ks.navigateTo({
                                url: `/pages/map/map?lat=${data.wd}&lng=${data.jd}&addr=${data.address}&tel=${data.tel}`,
                            });
                        else {
                            data.showAddr = true;
                            this.setData({
                                active: this.data.active,
                            });
                        }
                    } else if (this.data.tabIndex == 1) {
                        if (data.com_jd && data.com_wd)
                            ks.navigateTo({
                                url: `/pages/map/map?lat=${data.com_wd}&lng=${data.com_jd}&addr=${data.com_addr}&tel=${data.com_tel}`,
                            });
                        else {
                            data.showAddr = true;
                            this.setData({
                                active: this.data.active,
                            });
                        }
                    }
                }, 500);
            },
        });
    },

    areaChange(e) {
        let index = e.detail.value;
        this.setData({
            areaIndex: index,
        });
        this.getInfo();
    },

    jingquChange(e) {
        let index = e.detail.value;
        this.setData({
            jingquIndex: index,
        });
        this.getInfo();
    },

    copy(e) {
        let data = util.getValue("text", e);
        ks.setClipboardData({
            data,
        });
    },

    swiperNaiv(e) {
        let index = util.getValue("index", e);
        let data = this.data.lunbo[index];
        if (data) {
            if (data.lunbo_arr.id)
                ks.navigateTo({
                    url: `/pages/article/detail/detail?id=${data.lunbo_arr.id}&type=${data.lunbo_arr.type}&city=${data.citycode}`,
                });
            else {
                if (data.path)
                    ks.navigateTo({
                        url: `/${data.path}`,
                    });
                else {
                    this.setData({
                        showtk: true
                    })
                    util.navContact(data.name, data.lunbo_arr.url)
                }
            }
        }
    },

    onShareAppMessage: function () {

    },
});